<template>
  <div>
    <el-dialog width="80%" :visible.sync="dialogVisible" @close="onClose" :title="title">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="250px"
               label-position="left">
        <el-form-item  label="投入金币" prop="gameAmount">
          <el-input v-model="formData.gameAmount" placeholder="请输入投入金币"></el-input>
        </el-form-item>
        <el-form-item  label="类型" prop="type">
          <el-radio-group v-model="formData.type">
            <el-radio :label="1">俩间房</el-radio>
            <el-radio :label="2">五间房</el-radio>
            <el-radio :label="3">八间房</el-radio>
            <el-radio :label="4">石头剪刀布</el-radio>
            <el-radio :label="5">炸飞机</el-radio>
            <el-radio :label="6">大富翁</el-radio>
            <el-radio :label="7">爬塔</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import {gameConfigSave,gameConfigUpdate} from "@/api/game/game"

export default {
  data() {
    return {
      dialogVisible: false,
      title: '编辑',
      formData: {
        gameAmount: undefined,
        type: 1,
      },
      rules: {
        gameAmount: [{
          required: true,
          message: '请输入投入金币',
          trigger: 'blur'
        }],
      },
      id:null
    }
  },

  methods: {
    // 弹窗
    async onOpen(obj) {
      if(obj){
        this.id = true
        this.title="编辑"
        this.formData = JSON.parse(JSON.stringify(obj))
      }else{
        this.id = false
        this.title="新增"
      }
      this.dialogVisible = true
    },
    onClose() {
      this.dialogVisible = false
      this.$refs['elForm'].resetFields()
      this.formData.destroynumber = undefined
      this.formData.energyPerStamina = undefined
      this.formData.outputenerg = undefined
      this.formData.synthesisRate = undefined
    },
    close() {
      this.dialogVisible = false
    },
    // 提交数据
    handleConfirm() {
      this.$refs['elForm'].validate(async valid => {
        this.loading = true
        if (!valid) {
          // 如果验证不通过，则阻止后续代码执行
          this.loading = false;
          return;
        }
        if (this.id) {
          try {
            // 编辑
            await gameConfigUpdate(this.formData)
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.$emit('success')
            this.onClose()
            this.loading = false
          } catch (error) {
            this.loading = false
          }
        } else {
          try {
            // 新增
            await gameConfigSave(this.formData)
            this.$message({
              message: '新增成功',
              type: 'success'
            });
            this.$emit('success')
            this.onClose()
            this.loading = false
          } catch (error) {
            this.loading = false
          }
        }
      })
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
